#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
    printf("请输入数组长度");
    int n = 0;
    scanf("%d",&n);
    int *p = (int*)malloc(n*sizeof(int));
    memset(p,0,n*sizeof(int));
    if(p == NULL)
    {
        exit(-1);
    }
    int cou = 0;
    int num = 0;
    for(;; )
    {
        printf("\n请输入一个数字");
        scanf("%d",&num);
        if(num == -1)
        {
           break;
        }

        if(cou == n)
        {
           int *p1 = (int*)malloc(2*n*sizeof(int));//在堆内存开辟一个2n的空间
           if(p1 == NULL)
           {
            exit(-1);
           }
           memcpy(p1,p,n*sizeof(int));//拷贝空间
           free(p);//释放空间
           //n = 2*n;//令n=2n
           p = p1;//定义新指针p指向p1
        }
        p[cou] = num;
        cou++;
    
    for(int i =0 ;i < n;i++)
    {
        printf("%d ",p[i]);
        
    }
    
    }
    free(p);
}

